package com.tc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tc.domain.Disease_diagnosis;
import com.tc.dto.Disease_diagnosisDTO;
import com.tc.dto.Disease_diagnosisQueryDTO;
import com.tc.mapper.Disease_diagnosisMapper;
import com.tc.service.IDisease_diagnosisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tc.vo.Disease_diagnosisVO;
import com.tc.vo.PageResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 疾病诊断服务实现类
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@Service
public class Disease_diagnosisServiceImpl extends ServiceImpl<Disease_diagnosisMapper, Disease_diagnosis> implements IDisease_diagnosisService {

    @Override
    public PageResultVO<Disease_diagnosisVO> pageQuery(Disease_diagnosisQueryDTO queryDTO) {
        // 构建查询条件
        LambdaQueryWrapper<Disease_diagnosis> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加模糊查询条件
        if (StringUtils.hasText(queryDTO.getIcdCode())) {
            queryWrapper.like(Disease_diagnosis::getIcdCode, queryDTO.getIcdCode());
        }
        
        if (StringUtils.hasText(queryDTO.getDiseaseCode())) {
            queryWrapper.like(Disease_diagnosis::getDiseaseCode, queryDTO.getDiseaseCode());
        }
        
        if (StringUtils.hasText(queryDTO.getDiseaseName())) {
            queryWrapper.like(Disease_diagnosis::getDiseaseName, queryDTO.getDiseaseName());
        }
        
        if (StringUtils.hasText(queryDTO.getDiseaseCategory())) {
            queryWrapper.eq(Disease_diagnosis::getDiseaseCategory, queryDTO.getDiseaseCategory());
        }
        
        // 执行分页查询
        Page<Disease_diagnosis> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        Page<Disease_diagnosis> resultPage = this.page(page, queryWrapper);
        
        // 转换为VO列表
        List<Disease_diagnosisVO> records = resultPage.getRecords().stream()
                .map(Disease_diagnosisVO::fromEntity)
                .collect(Collectors.toList());
        
        // 使用PageResultVO的静态方法直接构建分页结果对象
        return PageResultVO.convert(resultPage, records);
    }

    @Override
    public Disease_diagnosisVO getDiseaseById(Integer id) {
        Disease_diagnosis disease = this.getById(id);
        return Disease_diagnosisVO.fromEntity(disease);
    }

    @Override
    @Transactional
    public boolean addDisease(Disease_diagnosisDTO diseaseDTO) {
        Disease_diagnosis disease = new Disease_diagnosis();
        BeanUtils.copyProperties(diseaseDTO, disease);
        disease.setDeleted(0); // 设置为未删除
        
        return this.save(disease);
    }

    @Override
    @Transactional
    public boolean updateDisease(Disease_diagnosisDTO diseaseDTO) {
        Disease_diagnosis disease = new Disease_diagnosis();
        BeanUtils.copyProperties(diseaseDTO, disease);
        
        return this.updateById(disease);
    }

    @Override
    @Transactional
    public boolean deleteDisease(Integer id) {
        return this.removeById(id);
    }
    
    @Override
    @Transactional
    public boolean batchDeleteDisease(List<Integer> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public List<Disease_diagnosisVO> listByName(String name) {
        LambdaQueryWrapper<Disease_diagnosis> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(name)) {
            queryWrapper.like(Disease_diagnosis::getDiseaseName, name);
        }
        
        // 限制返回数量，防止返回过多数据
        queryWrapper.last("LIMIT 100");
        
        List<Disease_diagnosis> list = this.list(queryWrapper);
        
        return list.stream()
                .map(Disease_diagnosisVO::fromEntity)
                .collect(Collectors.toList());
    }
}
